#!/usr/bin/env bash

# curl -fsSL http://dev8.cn:8000/static/ops/centos-k8s1.sh | bash

host=$1
if [ -z "$host" ]
then
  host='http://dev8.cn:8000/static/ops'
fi


#单机版k8s
# https://blog.csdn.net/weixin_42182501/article/details/104347303

setenforce 0
systemctl stop firewalld
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
systemctl disable firewalld

swapoff -a

sed -i "s/\(^[^#].* swap .*$\)/#\1/" /etc/fstab


cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system

modprobe br_netfilter
lsmod | grep br_netfilter


yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager -y --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum -y install docker-ce-18.06.3.ce-3.el7 docker-ce-cli-18.06.3.ce-3.el7 containerd.io
systemctl start docker
systemctl enable docke



mkdir /etc/docker
cat <<EOF > /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://3laho3y3.mirror.aliyuncs.com"
  ]
}
EOF

systemctl restart docker


# docker-compose https://docs.docker.com/compose/install/
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose


cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum -y install kubelet-1.13* kubeadm-1.13* kubectl-1.13*
systemctl start kubelet
systemctl enable kubelet


docker pull mirrorgooglecontainers/kube-apiserver:v1.13.3
docker pull mirrorgooglecontainers/kube-controller-manager:v1.13.3
docker pull mirrorgooglecontainers/kube-scheduler:v1.13.3
docker pull mirrorgooglecontainers/kube-proxy:v1.13.3
docker pull mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd:3.2.24
docker pull coredns/coredns:1.2.6

docker tag mirrorgooglecontainers/kube-apiserver:v1.13.3 k8s.gcr.io/kube-apiserver:v1.13.3
docker tag mirrorgooglecontainers/kube-controller-manager:v1.13.3 k8s.gcr.io/kube-controller-manager:v1.13.3
docker tag mirrorgooglecontainers/kube-scheduler:v1.13.3 k8s.gcr.io/kube-scheduler:v1.13.3
docker tag mirrorgooglecontainers/kube-proxy:v1.13.3 k8s.gcr.io/kube-proxy:v1.13.3
docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag mirrorgooglecontainers/etcd:3.2.24 k8s.gcr.io/etcd:3.2.24
docker tag coredns/coredns:1.2.6 k8s.gcr.io/coredns:1.2.6


# kubeadm reset 如果重装。
kubeadm init --kubernetes-version=v1.13.3 --pod-network-cidr=10.100.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')

kubectl taint nodes --all node-role.kubernetes.io/master-

grep -q '^vm.swappiness=0$' /etc/sysctl.d/k8s.conf ||  ( echo -e 'vm.swappiness=0\n' >> /etc/sysctl.d/k8s.conf && echo 'setted' )
sysctl -p /etc/sysctl.d/k8s.conf

# 安装完毕，下面安装 kuboard
kubectl apply -f https://kuboard.cn/install-script/kuboard.yaml

# harbor
wget https://github.com/goharbor/harbor/releases/download/v1.10.4/harbor-online-installer-v1.10.4.tgz  -P /opt/docker -N

cd /opt/docker/
tar zxvf /opt/docker/harbor-online-installer-v1.10.4.tgz
cd harbor

wget $host/ops/harbor.yml -O /opt/docker/harbor/harbor.yml
./install.sh

# 查看token

echo "============================="
echo "安装完成!"
echo "k8s login token:"
kubectl -n kube-system get secret $(kubectl -n kube-system get secret | grep kuboard-user | awk '{print $1}') -o go-template='{{.data.token}}' | base64 -d

echo "kuboard url:"
echo "http://vm1:32567/login"

